package com.RedRiver.hospitalsystem.finance_service.app.controller;


import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.finance_service.client.BillClient;
import com.RedRiver.hospitalsystem.finance_service.client.param.*;
import com.RedRiver.hospitalsystem.finance_service.common.Finance_serviceResultCode;
import com.RedRiver.hospitalsystem.finance_service.common.WoniuxyBeanCopyUtil;
import com.RedRiver.hospitalsystem.finance_service.common.exception.BillException;
import com.RedRiver.hospitalsystem.finance_service.infra.model.Bill;
import com.RedRiver.hospitalsystem.finance_service.service.BillService;
import com.RedRiver.hospitalsystem.finance_service.service.from.*;
import com.RedRiver.hospitalsystem.finance_service.service.info.FindBillAllInfo;
import com.RedRiver.hospitalsystem.finance_service.service.info.FindBillAllListInfo;
import com.RedRiver.hospitalsystem.result.Result;
import com.alipay.api.AlipayApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@RestController
@RequestMapping("bill")
@Api(tags = "账单")
public class BillController implements BillClient {

    @Resource
    private BillService billService;
    //模拟医保支付
    @ApiOperation("模拟医保支付")
    @GetMapping("moniYibao")
    public String moniYibao(Integer id) {
        return billService.moniYibao(id);
    }

    //新建账单
    @ApiOperation("新建账单")
    @PostMapping("/createBill")
    public Result<FindBillAllInfoClient> createBill(@RequestBody CreateBillFromClient createBillFromClient) {
        CreateBillFrom copyone = WoniuxyBeanCopyUtil.copyone(createBillFromClient, CreateBillFrom.class);
        FindBillAllInfo findBillAllInfo=billService.createBill(copyone);
        FindBillAllInfoClient copyone1 = WoniuxyBeanCopyUtil.copyone(findBillAllInfo, FindBillAllInfoClient.class);
        return Result.getInstance(FindBillAllInfoClient.class).setData(copyone1);

    }

    //分页查询所有账单
    @ApiOperation("分页查询所有账单")
    @PostMapping("/findBillAll")
    public Result<FindBillAllListInfoClient> findBillAll(@RequestBody FindBillAllFormClient findBillAllFormClient) {
        FindBillAllForm copyone = WoniuxyBeanCopyUtil.copyone(findBillAllFormClient, FindBillAllForm.class);
        FindBillAllListInfo findBillAllListInfo=billService.findBillAll(copyone);
        FindBillAllListInfoClient copyone1 = WoniuxyBeanCopyUtil.copyone(findBillAllListInfo, FindBillAllListInfoClient.class);
        return Result.getInstance(FindBillAllListInfoClient.class).setData(copyone1);
    }

    //根据checkId查账单
    @ApiOperation("根据checkId查账单")
    @GetMapping("/findBillByCheckId")
    public Result<FindBillAllInfoClient> findBillByCheckId(@ApiParam("请输入挂号id") @RequestParam("id")Integer id) {
        System.out.println(id);
        FindBillAllInfo findBillAllInfo= billService.findBillByCheckId(id);
        FindBillAllInfoClient byIdInfo = BeanUtil.copyProperties(findBillAllInfo,FindBillAllInfoClient.class);
        return Result.getInstance(FindBillAllInfoClient.class).setData(byIdInfo);
    }


    //根据id查账单
    @ApiOperation("根据id查账单")
    @GetMapping("/findBillById")
    public Result<FindBillAllInfoClient> findBillById(@ApiParam("请输入账单id") @RequestParam("id")Integer id) {
        FindBillAllInfo findBillAllInfo = billService.getOneById(id);
        FindBillAllInfoClient byIdInfo = BeanUtil.copyProperties(findBillAllInfo,FindBillAllInfoClient.class);
        return Result.getInstance(FindBillAllInfoClient.class).setData(byIdInfo);
    }

    //根据id改账单
    @ApiOperation("根据id改账单")
    @PostMapping("/changeBillById")
    public Result<FindBillAllInfoClient> changeBillById(@RequestBody ChangeBillByIdFromClient changeBillByIdFromClient) {
        ChangeBillByIdFrom byIdFrom = WoniuxyBeanCopyUtil.copyone(changeBillByIdFromClient, ChangeBillByIdFrom.class);
        Bill bill = billService.changeBillById(byIdFrom);
        FindBillAllInfoClient byIdInfo = BeanUtil.copyProperties(bill,FindBillAllInfoClient.class);
        return Result.getInstance(FindBillAllInfoClient.class).setData(byIdInfo);
    }

    //订单支付
    @ApiOperation("订单支付")
    @ApiImplicitParam("订单支付")
    @GetMapping("aliPay")
    public String aliPay(@ApiParam("请输入账单id") @RequestParam("id")Integer id) throws AlipayApiException {
        return billService.aliPay(id);
    }

    //同步通知
    @ApiOperation("同步通知")
    @ApiImplicitParam("同步通知")
    @GetMapping("returnUrl")   //不做订单相关的事  页面跳转
    public Result<FindBillAllInfoClient> returnUrl() throws UnsupportedEncodingException {
        FindBillAllInfo findBillAllInfo=billService.returnUrl();
        FindBillAllInfoClient copyone1 = WoniuxyBeanCopyUtil.copyone(findBillAllInfo, FindBillAllInfoClient.class);
        return Result.getInstance(FindBillAllInfoClient.class).setData(copyone1);
    }

    //异步通知
    @ApiOperation("异步通知")
    @ApiImplicitParam("异步通知")
    @PostMapping("notyfyUrl")
    public void notyfyUrl() throws UnsupportedEncodingException {
        billService.notyfyUrl();
    }

    //未支付取消订单
    @ApiOperation("未支付取消订单")
    @ApiImplicitParam("未支付取消订单")
    @GetMapping("notOrder")
    public Result<String> notOrder(@ApiParam("请输入账单id") @RequestParam("id")Integer id){
        return billService.notOrder(id);
    }

    //已支付取消订单
    @ApiOperation("已支付取消订单")
    @ApiImplicitParam("已支付取消订单")
    @GetMapping("notYesOrder")
    public Result<String> notYesOrder(@ApiParam("请输入账单id") @RequestParam("id")Integer id)  throws Exception{
        return billService.notYesOrder(id);
    }

    //退款
    @ApiOperation("退款")
    @ApiImplicitParam("退款")
    @PostMapping("/toRefund")
    public String toRefund(@RequestBody RefundFromClient refundFromClient) throws Exception{

        RefundFrom refundFrom = WoniuxyBeanCopyUtil.copyone(refundFromClient, RefundFrom.class);
        return billService.toRefund(refundFrom, refundFromClient.getRequest(),refundFromClient.getResponse());
    }

}

